﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditStay : AdminPageBase
    {
        protected int Type = 0;

        private StayInfo _stayInfo = new StayInfo();
        private readonly StayController _stayController = new StayController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["Type"]))
            {
                Type = int.Parse(Request.QueryString["Type"]);
            }
            if (!String.IsNullOrEmpty(Request.QueryString["StayId"]))
                _stayInfo = _stayController.GetStay(int.Parse(Request.QueryString["StayId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadUser();
            LoadHotel();
            LoadSchedule();
            if (_stayInfo.StayId == -1) return;
            LoadPriceByHotelAndType();

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListStay.aspx?xml=Stay&Type=5");
        }

        private void SaveData()
        {
            var priceInfo = new PriceController().GetPrice(Convert.ToInt32(Request["ctl00$placeHolderContent$ddlPrice"]));
            _stayInfo.PriceId = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlPrice"]);
            _stayInfo.OffDate = DateTime.Parse(dteOffDate.Value);
            _stayInfo.OnDate = DateTime.Parse(dteOnDate.Value);
            _stayInfo.RoomQuantity = int.Parse(txtRoomQuantity.Text);
            TimeSpan ts = DateTime.Parse(dteOffDate.Value) - DateTime.Parse(dteOnDate.Value);
            _stayInfo.TotalPrice = priceInfo.SalePrice * ts.Days * int.Parse(txtRoomQuantity.Text);
            _stayInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _stayInfo.ScheduleId = int.Parse(ddlSchedule.SelectedValue);
            if (_stayInfo.StayId == -1)
            {
                _stayInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _stayInfo.CreatedDate = DateTime.Now;
                _stayController.InsertStay(_stayInfo);
            }
            else
            {
                _stayInfo.UpdatedBy = UserId;
                _stayInfo.UpdatedDate = DateTime.Now;
                _stayController.UpdateStay(_stayInfo);
            }

            Response.Redirect("/Pages/ListStay.aspx?xml=Stay&Type=5");
        }

        private void RenderForm()
        {
            var priceInfo = new PriceController().GetPrice(_stayInfo.PriceId);
            var roomInfo = new RoomController().GetRoom(priceInfo.ItemId);
            var hotelInfo = new HotelController().GetHotel(roomInfo.HotelId);
            ddlHotel.SelectedValue = hotelInfo.HotelId.ToString();
            ddlPrice.SelectedValue = priceInfo.PriceId.ToString();
            dteOnDate.Value = _stayInfo.OnDate.ToShortDateString();
            dteOffDate.Value = _stayInfo.OffDate.ToShortDateString();
            ddlStatus.SelectedValue = _stayInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _stayInfo.CreatedBy.ToString();
            txtRoomQuantity.Text = _stayInfo.RoomQuantity.ToString();
            ddlSchedule.SelectedValue = _stayInfo.ScheduleId.ToString();
        }

        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }

        private void LoadHotel()
        {
            ddlHotel.DataSource =
               new HotelController().ListHotel()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                   .OrderBy(p => p.HotelFullName);
            ddlHotel.DataTextField = "HotelFullName";
            ddlHotel.DataValueField = "HotelId";
            ddlHotel.DataBind();

            ddlHotel.Items.Insert(0, new ListItem("--- Chọn khách sạn ---", "-1"));
        }
        private void LoadPriceByHotelAndType()
        {
            var priceInfo = new PriceController().GetPrice(_stayInfo.PriceId);
            var roomInfo = new RoomController().GetRoom(priceInfo.ItemId);
            ddlPrice.DataSource = new PriceController().ListPriceByHotelAndType(roomInfo.HotelId);
            ddlPrice.DataTextField = "PriceNameByHotel";
            ddlPrice.DataValueField = "PriceId";
            ddlPrice.DataBind();

            ddlPrice.Items.Insert(0, new ListItem("--- Chọn loại giá phòng ---", "-1"));
        }
        private void LoadSchedule()
        {
            ddlSchedule.DataSource = new ScheduleController().ListSchedule().Where(p => p.Status == (int)Globals.DocumentStatus.Use).OrderBy(p => p.ScheduleName);
            ddlSchedule.DataTextField = "ScheduleName";
            ddlSchedule.DataValueField = "ScheduleId";
            ddlSchedule.DataBind();

            ddlSchedule.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }
    }
}